using System.Collections.Generic;
using UnityEngine;

public class LeaderBoardTypeGameUI : MonoBehaviour
{
	public enum TypeLeaderBoardByTypeGame
	{
		topAll = 0,
		topFriends = 1
	}

	[SerializeField]
	private TypeLeaderBoardByTypeGame typeLeaderBoard;

	[SerializeField]
	private PlayerInLeaderBoard ohterPlacePlayer;

	[SerializeField]
	private List<PlayerInLeaderBoard> placeNumberPlayer;

	private List<GameObject> _players = new List<GameObject>();

	private bool _init;

	public void Show(TypeGame typeGame)
	{
		if (!_init)
		{
			InitButtons(typeGame);
		}
	}

	private void InitButtons(TypeGame typeGame)
	{
		List<LeaderInfo> leadersByTypeGame = LeaderBoardParser.GetInstance().GetLeadersByTypeGame(typeGame, typeLeaderBoard);
		int num = 0;
		UIGrid component = GetComponent<UIGrid>();
		foreach (LeaderInfo item in leadersByTypeGame)
		{
			PlayerInLeaderBoard playerInLeaderBoard = ((item.place <= 0 || item.place > placeNumberPlayer.Count) ? Object.Instantiate(ohterPlacePlayer) : Object.Instantiate(placeNumberPlayer[item.place - 1]));
			playerInLeaderBoard.Init(item);
			playerInLeaderBoard.gameObject.SetActive(true);
			playerInLeaderBoard.transform.SetParent(component.transform);
			playerInLeaderBoard.transform.localScale = Vector3.one;
			if (Mathf.Abs(item.place - num) > 1)
			{
				playerInLeaderBoard.TurnOnSplitter();
			}
			num = item.place;
			_players.Add(playerInLeaderBoard.gameObject);
		}
		component.repositionNow = true;
		_init = true;
	}

	public void Clear()
	{
		foreach (GameObject player in _players)
		{
			Object.Destroy(player);
		}
		_players.Clear();
		_init = false;
	}
}
